<template>
  <div class="app-container">
    <el-form v-show="showSearch" ref="queryForm" :inline="true" :model="queryParams">
      <el-form-item label="容器类型编号" prop="containerTypeCode">
        <el-input
          v-model="queryParams.containerTypeCode"
          clearable
          placeholder="请输入容器类型编号"
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="容器类型名称" prop="containerTypeName">
        <el-input
          v-model="queryParams.containerTypeName"
          clearable
          placeholder="请输入容器类型名称"
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="容器类型层级" prop="containerTypeLevel">
        <el-input
          v-model="queryParams.containerTypeLevel"
          clearable
          placeholder="请输入容器类型层级"
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button icon="el-icon-search" size="mini" type="primary" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['biobank:containerType:add']"
          icon="el-icon-plus"
          plain
          size="mini"
          type="primary"
          @click="handleAdd"
        >新增
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="containerTypeList"
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
      default-expand-all
      row-key="containerTypeId"
    >
      <el-table-column label="容器类型编号" prop="containerTypeCode"/>
      <el-table-column align="center" label="容器类型名称" prop="containerTypeName"/>
      <el-table-column align="center" label="图标" prop="icon" width="100">
        <template v-if="scope.row.containerTypeIcon != undefined" slot-scope="scope">
          <svg-icon :icon-class="scope.row.containerTypeIcon"/>
        </template>
      </el-table-column>
      <el-table-column align="center" label="排序" prop="containerTypeSort"/>
      <el-table-column align="center" label="容器类型层级" prop="containerTypeLevel"/>
      <el-table-column align="center" label="备注" prop="containerTypeRemark"/>
      <el-table-column align="center" class-name="small-padding fixed-width" label="操作">
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['biobank:containerType:edit']"
            icon="el-icon-edit"
            size="mini"
            type="text"
            @click="handleUpdate(scope.row)"
          >修改
          </el-button>
          <el-button
            v-hasPermi="['biobank:containerType:remove']"
            icon="el-icon-delete"
            size="mini"
            type="text"
            @click="handleDelete(scope.row)"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改容器类型对话框 -->
    <el-dialog :title="title" :visible.sync="open" append-to-body width="500px">
      <el-form ref="form" :model="form" :rules="rules" label-width="110px">
        <el-form-item label="父容器类型" prop="parentId">
          <treeselect v-model="form.parentId" :normalizer="normalizer" :options="containerTypeOptions"
                      placeholder="请选择父容器类型"
          />
        </el-form-item>
        <el-form-item label="容器类型编号" prop="containerTypeCode">
          <el-input v-model="form.containerTypeCode" placeholder="请输入容器类型编号"/>
        </el-form-item>
        <el-form-item label="容器类型名称" prop="containerTypeName">
          <el-input v-model="form.containerTypeName" placeholder="请输入容器类型名称"/>
        </el-form-item>
        <el-form-item label="菜单图标">
          <el-popover
            placement="bottom-start"
            trigger="click"
            width="460"
            @show="$refs['iconSelect'].reset()"
          >
            <icon-select ref="iconSelect" @selected="selected"/>
            <el-input slot="reference" v-model="form.containerTypeIcon" placeholder="点击选择图标" readonly>
              <svg-icon
                v-if="form.containerTypeIcon"
                slot="prefix"
                :icon-class="form.containerTypeIcon"
                class="el-input__icon"
                style="height: 32px;width: 16px;"
              />
              <i v-else slot="prefix" class="el-icon-search el-input__icon"/>
            </el-input>
          </el-popover>
        </el-form-item>
        <el-form-item label="排序" prop="containerTypeSort">
          <el-input-number v-model="form.containerTypeSort" :min="1" controls-position="right" placeholder="请输入排序"
                           style="width: 100%"
          />
        </el-form-item>
        <el-form-item label="容器类型层级" prop="containerTypeLevel">
          <el-input-number v-model="form.containerTypeLevel" :min="1" controls-position="right" placeholder="请输入容器类型层级"
                           style="width: 100%"
          />
        </el-form-item>
        <el-form-item label="备注" prop="containerTypeRemark">
          <el-input v-model="form.containerTypeRemark" placeholder="请输入备注"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  addContainerType,
  delContainerType,
  getContainerType,
  listContainerType,
  updateContainerType
} from '@/api/biobank/containerType'
import Treeselect from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
import IconSelect from '@/components/IconSelect'

export default {
  name: 'ContainerType',
  components: {
    Treeselect,
    IconSelect
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 容器类型表格数据
      containerTypeList: [],
      // 容器类型树选项
      containerTypeOptions: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        containerTypeCode: null,
        containerTypeName: null,
        containerTypeLevel: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        parentId: { required: true, trigger: 'change', message: '父容器类型不能为空' },
        containerTypeCode: [{ required: true, trigger: 'blur', message: '容器类型编号不能为空' }, {
          max: 20,
          trigger: 'blur',
          message: '容器类型编号不能超过20个字符'
        }],
        containerTypeName: [{ required: true, trigger: 'blur', message: '容器类型名称不能为空' }, {
          max: 20,
          trigger: 'blur',
          message: '容器类型名称不能超过20个字符'
        }],
        containerTypeLevel: { required: true, trigger: 'blur', message: '容器类型层级不能为空' },
        containerTypeRemark: { max: 64, trigger: 'blur', message: '备注不能超过64个字符' }
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    // 选择图标
    selected(name) {
      this.form.containerTypeIcon = name
    },
    /** 查询容器类型列表 */
    getList() {
      this.loading = true
      listContainerType(this.queryParams).then(response => {
        this.containerTypeList = this.handleTree(response.data, 'containerTypeId', 'parentId')
        this.loading = false
      })
    },
    /** 转换容器类型数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children
      }
      return {
        id: node.containerTypeId,
        label: node.containerTypeName,
        children: node.children
      }
    },
    /** 查询容器类型下拉树结构 */
    getTreeselect() {
      listContainerType().then(response => {
        this.containerTypeOptions = []
        const data = { containerTypeId: 0, containerTypeName: '顶级节点', children: [] }
        data.children = this.handleTree(response.data, 'containerTypeId', 'parentId')
        this.containerTypeOptions.push(data)
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        containerTypeId: null,
        parentId: null,
        containerTypeCode: null,
        containerTypeName: null,
        containerTypeSort: null,
        containerTypeLevel: null,
        containerTypeRemark: null,
        containerTypeIcon: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.getTreeselect()
      this.open = true
      this.title = '添加容器类型'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      this.getTreeselect()
      if (row != null) {
        this.form.parentId = row.containerTypeId
      }
      getContainerType(row.containerTypeId).then(response => {
        this.form = response.data
        this.open = true
        this.title = '修改容器类型'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.containerTypeId != null) {
            updateContainerType(this.form).then(response => {
              this.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addContainerType(this.form).then(response => {
              this.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$confirm('是否确认删除容器类型 编号为"' + row.containerTypeId + '"的数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return delContainerType(row.containerTypeId)
      }).then(() => {
        this.getList()
        this.msgSuccess('删除成功')
      })
    }
  }
}
</script>
